// pages/vip-buy/vip-buy.js
import { getVIPTypes, buyVIP, VIPPayUpdateOrder } from '../../api/apis.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._initData()
  },
  _initData() {
    getVIPTypes({
      data: {},
      success: res => {
        if (res.state) {
          this.setData({
            bgImage: res.data.image,
            vipTypes: res.data.sku
          })
        }
      }
    })
  },
  buyVip(e) {
    wx.showLoading({
      title: '正在发起支付...',
      mask: true
    })
    buyVIP({
      data: {
        openid: app.globalData.openid,
        title: e.currentTarget.dataset.item.title,
        remark: ''
      },
      success: res => {
        wx.hideLoading()
        if (res.state) {
          let params = JSON.parse(res.data.parameters)
          if (parseFloat(e.currentTarget.dataset.item.Spends) > 0) {
            wx.requestPayment({
              nonceStr: params.nonceStr,
              package: params.package,
              paySign: params.paySign,
              signType: params.signType,
              timeStamp: params.timeStamp,
              success: response => {
                wx.showToast({
                  title: '支付成功',
                  mask: true,
                  duration: 2000,
                  success: (e) => {
                    // setTimeout(() => {
                    //   wx.navigateTo({
                    //     url: '../order-list/order-list',
                    //   })
                    // }, 2000)
                  }
                })
              },
              complete: response => {
                if (response.errMsg == "requestPayment:cancel" || response.errMsg == "requestPayment:fail cancel") {
                  this._updateOrder(res, 'fail')
                }
              }
            })
          } else {
            this._updateOrder(res, 'ok')
          }
        } else {
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 4000
          })
        }
      }
    })
  },
  _updateOrder(res, state) {
    VIPPayUpdateOrder({
      data: {
        orderid: res.data.out_trade_no,
        payStatus: state,
        payReamrk: '',
        openid: app.globalData.openid
      },
      success: res => {
        if (res.state) {
          wx.showToast({
            title: state == 'ok' ? '支付成功' : '支付失败',
            icon: 'none',
            mask: true,
            duration: 2000,
            success: (e) => {
              // setTimeout(() => {
              //   wx.navigateTo({
              //     url: '../order-list/order-list',
              //   })
              // }, 2000)
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})